06一个JAVA小程序 求解``````

来源:百度知道 编辑:UC知道 时间:2024/06/08 06:19:29
6.编写应用程序,其中创建一个文本区,两个按钮,并将两个按钮添加到面
板中。对文本区和面板设置为2行1列的GridLayout布局。,给在文本区中显示你的名字,单击第二个按钮时,清除文本区中的内容。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class test extends JFrame{
private JTextField tf = new JTextField();
private JPanel pBtn = new JPanel(new GridLayout(1,2));
private JButton btn1 = new JButton("显示自己姓名");
private JButton btn2 = new JButton("清除自己姓名");
public test() {
super("一个JAVA小程序06");
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void init(){
GridLayout layout = new GridLayout(2,1);
Container c = getContentPane();
c.setLayout(layout);

pBtn.add(btn1);
pBtn.add(btn2);
c.add(tf);
c.add(pBtn);

uiHandler handler = new uiHandler();
btn1.addActionListener(handler);
btn2.addActionListener(handler);
}

public static void main(String args[]) {
test application = n